package com.neusoft.po;

import com.neusoft.util.CommonUtil;

import java.util.ArrayList;
import java.util.List;

public class RedPacketList {
    private List<RedPacket> list;
    private List<RedPacket> expiredList = new ArrayList<>();

    public List<RedPacket> getDeletedList() {
        return deletedList;
    }

    private List<RedPacket> deletedList = new ArrayList<>();
    private RedPacket updatedRedPacket;


    public RedPacketList(List<RedPacket> list) {
        this.list = list;
    }

    public List<RedPacket> getExpiredList() {
        return expiredList;
    }

    public RedPacket getUpdatedRedPacket(Double left) {
        //将list中所有credit按expiredTime升序排序
        list.sort((o1, o2) -> {
            if (CommonUtil.compareDate(o1.getExpiredTime(), o2.getExpiredTime()) == 1)
                return 1;
            else if (CommonUtil.compareDate(o1.getExpiredTime(), o2.getExpiredTime()) == -1)
                return -1;
            else
                return 0;
        });
        //遍历list,将积分从最早过期的开始扣除,直到left<=0
        for (RedPacket redPacket1 : list) {
            if (left == 0)
                break;
            if (CommonUtil.compareDate(redPacket1.getExpiredTime(), CommonUtil.getCurrentDate1()) == 1) {
                if (redPacket1.getNum() >= left) {
                	redPacket1.setNum(redPacket1.getNum() - left);
                    left = 0.0;
                    updatedRedPacket = redPacket1;
                } else {
                    left -= redPacket1.getNum();
                    deletedList.add(redPacket1);
                }
            }
        }
        return updatedRedPacket;
    }
}
